package com.wiscamp.ninechapters.accounts.domain.services;

import com.wiscamp.ninechapters.accounts.domain.models.OrderPayment;
import com.wiscamp.ninechapters.accounts.domain.repositories.PaymentsRepository;
import cube.ddd.aspect.AopServiceException;
import com.wiscamp.ninechapters.ddd.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PaymentsService implements DomainService {

    @Autowired
    private PaymentsRepository paymentRepository;

    public PaymentsService(PaymentsRepository paymentRepository) {
        this.paymentRepository = paymentRepository;
    }

    @AopServiceException
    public OrderPayment createOrderPayment(long userId, String paymentType, String transactionNumber, String goodsCode, double amount) {
        OrderPayment payment = new OrderPayment();
        payment.setPaymentType(paymentType);
        payment.setUserId(userId);
        payment.setAmount(amount);
        payment.setTransactionNumber(transactionNumber);
        payment.setGoodsCode(goodsCode);
        payment.setOrderTime(LocalDateTime.now());

        return paymentRepository.saveOrderPayment(payment);
    }

    @AopServiceException
    public OrderPayment getOrderPayment(String transactionNumber) {
        return paymentRepository.getOrderPayment(transactionNumber);
    }

    @AopServiceException
    public OrderPayment getOrderPayment(long orderId) {
        return paymentRepository.getOrderPayment(orderId);
    }

    @AopServiceException
    public OrderPayment updateOrderPayment(OrderPayment orderPayment) {
        return paymentRepository.saveOrderPayment(orderPayment);
    }
}
